package com.java.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.java.entity.Comment;
import com.java.utils.SqlCons;
import com.java.utils.UserSession;
/**
* @ClassName: CommentDao
* @Description: 留言数据库基础操作类
* @author ChenJiaLu
* @date 2023年12月29日 上午11:05:55
*
 */
public class CommentDao extends BaseDao {

	public UserSession session = UserSession.getInstance();

	public List<Comment> getDoctorComments(){
		List<Comment> list = new ArrayList<Comment>();
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.COMMENT.SELECT_DOCTOR);
			statement.setInt(1, session.getUserId());
			ResultSet res = statement.executeQuery();
			while(res.next()) {
				Comment info = new Comment();
				info.setId(res.getInt("id"));
				info.setDoctorid(res.getInt("doctorid"));
				info.setPatientid(res.getInt("patientid"));
				info.setContent(res.getString("content"));
				info.setReply(res.getString("reply"));
				
				PreparedStatement doctorSt = getConnection().prepareStatement(SqlCons.DOCTOR.SELECT_INFO);
				doctorSt.setInt(1, res.getInt("doctorid"));
				ResultSet doctorRes = doctorSt.executeQuery();
				if(doctorRes.next()) {
					info.setDoctorName(doctorRes.getString("name"));
				}
				PreparedStatement patientSt = getConnection().prepareStatement(SqlCons.PATIENT.SELECT_INFO);
				patientSt.setInt(1, res.getInt("patientid"));
				ResultSet patientRes = patientSt.executeQuery();
				if(patientRes.next()) {
					info.setPatientName(patientRes.getString("name"));
				}
				list.add(info);
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
		return list;
	}

	public List<Comment> getPatientComments(){
		List<Comment> list = new ArrayList<Comment>();
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.COMMENT.SELECT_PATIENT);
			statement.setInt(1, session.getUserId());
			ResultSet res = statement.executeQuery();
			while(res.next()) {
				Comment info = new Comment();
				info.setId(res.getInt("id"));
				info.setDoctorid(res.getInt("doctorid"));
				info.setPatientid(res.getInt("patientid"));
				info.setContent(res.getString("content"));
				info.setReply(res.getString("reply"));
				
				PreparedStatement doctorSt = getConnection().prepareStatement(SqlCons.DOCTOR.SELECT_INFO);
				doctorSt.setInt(1, res.getInt("doctorid"));
				ResultSet doctorRes = doctorSt.executeQuery();
				if(doctorRes.next()) {
					info.setDoctorName(doctorRes.getString("name"));
				}
				PreparedStatement patientSt = getConnection().prepareStatement(SqlCons.PATIENT.SELECT_INFO);
				patientSt.setInt(1, res.getInt("patientid"));
				ResultSet patientRes = patientSt.executeQuery();
				if(patientRes.next()) {
					info.setPatientName(patientRes.getString("name"));
				}
				
				list.add(info);
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
		return list;
	}

	public void delComment(int id) {
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.COMMENT.DELETE);
			statement.setInt(1, id);
			statement.executeUpdate();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
	}

	public void saveComment(Comment comment) {
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.COMMENT.INSERT);
			statement.setInt(1, comment.getDoctorid());
			statement.setInt(2, comment.getPatientid());
			statement.setString(3, comment.getContent());
			statement.setString(4, comment.getReply());
			statement.executeUpdate();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
	}

	public void updateComment(Comment comment) {
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.COMMENT.UPDATE);
			statement.setInt(1, comment.getDoctorid());
			statement.setInt(2, comment.getPatientid());
			statement.setString(3, comment.getContent());
			statement.setString(4, comment.getReply());
			statement.setInt(5, comment.getId());
			statement.executeUpdate();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
	}


}
